<?php
/**
 * -----------------------------------------------------
 *
 * models menu_model
 *
 * @package     models
 * @subpackage  
 * @category    menu_model  -  菜单模型类
 * @author      ChenFu
 * @since       2013-12-4 version 1.0       
 * -----------------------------------------------------    
 */
class Menu_model extends CI_Model
{    
	public static $error;            // 错误信息
	const TABLE = 'xc_system_menus'; // 表名
	private static $str_options; 	// 菜单分类
	private static $to_list;
	private static $str_pop;

	public function __construct()
	{
		parent::__construct();
        $this->load->database();
	}

	/**
	 * 增加菜单
	 */
	public function insert($data)
	{
		unset($data['x']);unset($data['y']);
		if($this->db->insert(self::TABLE,$data)){
			return true;
		}else{
			return false;
		}
	}

   
	/**
	 * 查询所有的菜单
	 */
	public function get_all_menu()
	{
		$sql = 'select * from '.self::TABLE.' order by menu_id desc';
		$query=$this->db->query($sql);
		if($query->num_rows()){
			return $query->result_array();
		}
	}

    //根据父级分类 查询子分类
    public  function get_son_menu_by_pid($pid=false)
    {
        $sql = "select * from xc_system_menus where menu_pid = ".$pid." order by menu_sort asc";
        $query = $this->db->query($sql);
        if($query->num_rows() > 0){
            return $query->result_array();
        }
    }

    //生成下拉菜单形式的数据（option）
    public function to_options($start,$level,$selected_val=null,$clear_str=false)
    {
       if($clear_str)
       {
            self::$str_options='';
       }
       $intend=$level*14;
       //查询数据
       $arr = $this->get_son_menu_by_pid($start);
       if(is_array($arr)){
            foreach($arr as $rows){
                if($selected_val == $rows['menu_id']){
                    self::$str_options.='<option value="'.$rows['menu_id'].'"  selected="selected"  style="text-indent:'.$intend.'px;">|_&nbsp;'.$rows['menu_name'].'</option>';
                }else{
                   self::$str_options.='<option value="'.$rows['menu_id'].'"  style="text-indent:'.$intend.'px;">|_&nbsp;'.$rows['menu_name'].'</option>';
                }
                //递归（自己调用自己）
                $this->to_options($rows['menu_id'],$start+1,$selected_val);
            }
       }
       return self::$str_options;
    }

    //生成列表形式的分类信息
    public function to_list($start,$level,$clear_str=false)
    {
        if($clear_str){
           self::$to_list = '';
        }
        $indent = $level*20;
        $arr    = $this->get_son_menu_by_pid($start);
        if(is_array($arr)){
        	foreach($arr as $rows){
	            self::$to_list .= '<tr class="text-c">
	            <td><input type="checkbox" name="" value=""></td>
	            <td>'.$rows['menu_id'].'</td>
	            <td>'.$rows['menu_sort'].'</td>
				<td  class="text-l" style="text-indent:'.$indent.'px;">|_'.$rows['menu_name'].'</td>
				<td width="8%">
		            <a title="编辑" href="menu/update?mid='.$rows['menu_id'].'" style="text-decoration:none"><i class="icon-edit"></i></a>
	           		<a title="删除" href="javascript:;"  onclick="return delmenu('.$rows['menu_id'].');" class="ml-5" style="text-decoration:none"><i class="icon-trash"></i></a>
	            </td>
			  </tr>';
	          $this->to_list($rows['menu_id'],$level+1); //递归
        	}
        }
        return  self::$to_list;
    }

	/**
	 * 根据父级分类获取菜单
	 * @param $start
	 * @return mixed
	 */
	public function  get_all_pop($start){
		if(isset($start)){ // 判断$start是否存在
			$sql = 'select * from '.self::TABLE.' where menu_pid = '.$start.' and menu_state = 1  order by menu_id asc';
		}else{
			$sql = 'select * from '.self::TABLE.' where menu_state = 1   order by menu_id asc';
		}
		$query=$this->db->query($sql);
		if($query->num_rows()){
			return $query->result_array();
		}
	}

	/**
	 * 编辑用户信息
	 */
	public function update($data,$id)
	{
		unset($data['x']);unset($data['y']);
		if($this->db->update(self::TABLE,$data,'menu_id = '.$id)){
			return true;
		}else{
			return false;
		}
	}

	/**
	 * 根据id查询菜单信息
	 */
	public function get_menu_by_menu_id($menu_id)
	{
		$this->db->where('menu_id',$menu_id);
		$query = $this->db->get(self::TABLE);
		return $query->row_array();
	}

	public function get_pid_date($pid){
		$this->db->where('menu_pid',$pid);
		$query = $this->db->get(self::TABLE);
		return $query->result_array();
	}

	/**
	 * 根据id删除数据
	 */
	public function delete($id)
	{
		$this->db->where('menu_id',$id);
		if($this->db->delete(self::TABLE)){
			return true;
		}else{
			return false;
		}
	}

}